import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Scanner;

/**
 * Created by yanli on 16/7/29.
 */
public class FileUtils {
    public static final Logger logger = LoggerFactory.getLogger(Common.class);

    public static String getFileContent(String path){
        File file = new File(path);
        StringBuffer stringBuffer = new StringBuffer();
        try {
            Scanner scanner = new Scanner(file, "UTF-8");
            while (scanner.hasNext()){
                stringBuffer.append(scanner.next());
            }
        } catch (FileNotFoundException e) {
            logger.debug("路径"+path+"找不到!");
        }
        return stringBuffer.toString();
    }

    public static String getFileJson2(String path){
        File file = new File(path);
        String josnString = "";
        FileInputStream fileInputStream = null;
        InputStreamReader inputStreamReader = null;
        try {
            fileInputStream = new FileInputStream(file);
            try {
                inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String str;
            try {
                while ((str = reader.readLine()) != null){
                    josnString += str;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStreamReader != null){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return josnString;
    }

    public static String getFileContent3(String filePath){
        StringBuffer stringBuffer = new StringBuffer();
        File file = null;
        if (StringUtils.isNotBlank(filePath)){
            file = new File(filePath);
        }else {
            logger.error("文件路径不存在!");
        }

        if (file.exists()){
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(file);
                byte[] b = new byte[256];
                try {
                    while (fileInputStream.read(b) != -1){
                        stringBuffer.append(new String(b, "UTF-8"));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally {
                if (fileInputStream != null){
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return stringBuffer.toString();
        }else {
            return null;
        }
    }
}
